package com.xb.p4_widgets;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RadioButtonActivity extends AppCompatActivity {

    TextView tv_sex;
    RadioButton r1;
    RadioButton r2;
    EditText name;
    Button btn_OK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        tv_sex=findViewById(R.id.tv_sex);
        name=findViewById(R.id.name);
        r1=findViewById(R.id.boy);
        r2=findViewById(R.id.girl);
        btn_OK=findViewById(R.id.btn_ok);
        RadioGroup sex=findViewById(R.id.sex);

        //按钮状态改变
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取具体被选中的按钮的值
                RadioButton r=findViewById(checkedId);
                tv_sex.setText("你选择了性别"+r.getText());
            }
        });

        //确认按钮的监听事件
        btn_OK.setOnClickListener(new myClick());

    }

    class myClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            CharSequence str="",name2="";
            name2=name.getText();
            if(r1.isChecked()){
                str= r1.getText();
            }
            if(r2.isChecked()){
                str= r2.getText();
            }

            tv_sex.setText("您输入的信息为：\n姓名："+name2+"\n性别："+str);


        }
    }

}